package com.bmadmin.controller.cus;

import com.bmadmin.domain.DataQueryItemVariablePOJO;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.service.DataQueryItemVariableService;
import com.bmadmin.util.BMUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 金玮良 on 2017-12-21.
 */
@RestController
@RequestMapping("/dataQueryItemVariable")
public class DataQueryItemVariableController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Autowired
    DataQueryItemVariableService dataQueryItemVariableService;

    @GetMapping("/list")
    public LayUiDataTablePOJO getList(DataQueryItemVariablePOJO dataQueryItemVariablePOJO){
        return dataQueryItemVariableService.getVariableList(dataQueryItemVariablePOJO);
    }

    @PostMapping(value="/insert",produces = "application/json")
    public NormalResult insertVariable(@RequestBody DataQueryItemVariablePOJO dataQueryItemVariablePOJO){
        NormalResult result = new NormalResult();

        String variableName=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_name());
        String variableType=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_type());
        String variableValue=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_value());

        if("".equals(variableName)||"".equals(variableType)||"".equals(variableValue)){
            result.setCode("9999");
            result.setMsg("传入参数不完整");
            return result;
        }

        result = dataQueryItemVariableService.insertQueryItemVariable(dataQueryItemVariablePOJO);
        return result;
    }

    @PostMapping(value="/update",produces = "application/json")
    public NormalResult updateVariable(@RequestBody DataQueryItemVariablePOJO dataQueryItemVariablePOJO){
        NormalResult result = new NormalResult();

        String variableid=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_id());
        String variableName=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_name());
        String variableType=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_type());
        String variableValue=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_value());

        if("".equals(variableName)||"".equals(variableType)||"".equals(variableValue)||"".equals(variableid)){
            result.setCode("9999");
            result.setMsg("传入参数不完整");
            return result;
        }

        result = dataQueryItemVariableService.updateQueryItemVariable(dataQueryItemVariablePOJO);
        return result;
    }

    @PostMapping("/delete")
    public NormalResult delVariable(DataQueryItemVariablePOJO dataQueryItemVariablePOJO){
        NormalResult result = new NormalResult();

        String variableid=BMUtil.NulltoString(dataQueryItemVariablePOJO.getV_id());

        if("".equals(variableid)){
            result.setCode("9999");
            result.setMsg("传入参数不完整");
            return result;
        }

        result = dataQueryItemVariableService.delQueryItemVariable(dataQueryItemVariablePOJO);
        return result;
    }

}
